use List::*;

enum List {
    Cons(u32,Box<List>), //contains the linked_list element and the pointer to nextNode
    Nil,    //末位节点
}

//添加方法
impl List {
    fn new()->List {
        Nil
    }

    //头插法
    fn prepend(self,elem: u32)->List {
        Cons(elem,Box::new(self))
    }

    fn len(&self) -> u32 {
        // `self` 为 `&List` 类型，`*self` 为 `List` 类型，匹配一个具体的 `T`
        // 类型要好过匹配引用 `&T`。
        match *self {
            Cons(_,ref tail)=> 1+ tail.len(),
            Nil=>0
        }
    }

    fn stringify(&self) -> String {
        match *self{
            Cons(head,ref tail) => {
                format!("{}, {}",head,tail.stringify())
            },
            Nil=> {
                format!("Nil")
            }
        }
    }

    fn for_each_print(&self) {

    }
}
pub fn example() {
    let mut list = List::new();

    list = list.prepend(1);
    list = list.prepend(2);
    list = list.prepend(3);

    println!("linked list has length: {}",list.len());
    println!("{}",list.stringify());
}